* ============================================================================================
* Date: December 2023
* Project: The Populist Backlash against Globalization: A Meta-Analysis of the Causal Evidence
*
* Dataset: Meta_dataset_final.dta
* 
*
* This file contains the code for the publication bias tests. The descriptive tables produced
* using the "vote counting" approach were produced in Excel, included in a separate file. 
*
* Note, before running the code, you need to set up the folder structure of your working 
* directory. Create a "Data" folder and place Meta_dataset_final.dta there, create a "Code" 
* folder for the do file, and create a "Results" folder for the results.
* 
* For further questions, contact Gabor Scheiring <gs1098@georgetown.edu>.
* ===========================================================================================


* Insert directory here:
global path ""

use "$path\Data\Meta_dataset_final.dta", clear

* Install packages
ssc install rddensity
ssc install outreg2
ssc install blindschemes
net install lpdensity, from(https://raw.githubusercontent.com/nppackages/lpdensity/master/stata) replace


* set format for figures
set scheme plotplain

* Generate partial correlation coefficient (pcc)

gen pcc=t/(sqrt((t^2)+(n-1)))

* Standard Error (SE) of partial correlation coefficient

gen pcc_SE= sqrt((1-pcc^2)/(n-1))

* Inverse of SE - Measure of precision
gen pcc_SE_inverse=1/pcc_SE

mean pcc
// mean pcc=0.0579


***********
* FIGURES*
***********

* Figure 4- Density  of the t-statistics

rddensity t, c(1.96)  vce(plugin)  plot  plot_range(-5 8) ///
graph_opt(xtitle("t-statistic") ytitle("Density") legend(off) xline(1.96, lcolor(red) lpattern(solid)) xlab(-5(5)10 1.96,nogrid) ylab(0 (.1) .5)) 
graph export "$path\Results\Figure 4.png", replace  width(1500) height(1200)

* test of no discontinuity 

rddensity t, c(1.96)  vce(plugin) 
// p-value:0.607 
// Table S4.1 comes from table P-values of binomial tests.
//    - Lower value=1.96 - (Window lenght/2)
//    - Higher value=1.96 + (Window lenght/2)


* Figure 5 - Inverse funnel plot

scatter pcc_SE_inverse pcc, xline(0.0579, lcolor(red) lpattern(solid)) ylab(,nogrid) xlab(-1(1)1)  ytitle("Precision (1/SE(r))" " ") ///
 xtitle("partial correlation coefficient (pcc)")

graph export "$path\Results\Figure 5.png", replace  width(1500) height(1500)


********************
**Funnel-Asymmetry Precision-Effect test regressions 
***************
* Generate weight: inverse of the squared standard error of the PCC

gen weight=1/((pcc_SE)^2)


* Generate IV for model 2: square root of number of observations
gen n_sqrt=sqrt(n)

* Generate square of SE for model 3
gen pcc_SE_sq=pcc_SE^2


* Table 3
 
* Model 1. WLS
regress pcc pcc_SE [aw  =weight ], vce (cluster id)
 outreg2 using "$path\Results\Table 4.xls", replace   tex(frag)  ctitle(Model (1)) stats(coef se)   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	

* Model 2. 2sls
 ivregress 2sls pcc   (pcc_SE=n_sqrt) [aw =weight ], first vce(cluster id)  
 outreg2 using "$path\Results\Table 4.xls", append   tex(frag)  ctitle(Model (2)) stats(coef se)   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	
	
*3. Square SE
regress pcc pcc_SE_sq [aw  =weight ], vce (cluster id)
 outreg2 using "$path\Results\Table 4.xls", append   tex(frag)  ctitle(Model (3)) stats(coef se)   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	





*****************************
*** HETEROGENEITY  ANALYSIS
*****************************

* Code variables 

* Encode outcome variable, treatment and methodology
encode estimateleveloutcome, gen (outcome)

encode primarytreatment, gen (treatment)

encode primaryexperimentaldesign, gen(methodology)

* Define region variable
 gen region=1 if country=="U.S."

replace region=2 if inlist(country,"Austria", "European Union", "France") 

replace region=2 if inlist(country, "Germany", "Hungary", "Italy", "Poland", "Sweden", "U.K.", "Western Europe")

replace region=3 if inlist(country,"Brazil")

label define regionl 1 "1.US" 2 "2.Europe" 3 "3.Brazil" 
label values region regionl

* Generate discipline groups

encode discipline, gen(discipline_b)

    gen discipline_g=discipline_b
  	replace discipline_g=2 if discipline_b==4 

   label define discipline_gl 1 "1 Economics" 2 "2 Interdisciplinary and Sociology" 3 "3 Political Science" 
   label values discipline_g discipline_gl

	
	
   * Table 5 	
	
	*  Model 1. Including economic & political variables

	 regress pcc pcc_SE ib3.outcome ib6.treatment [aw =weight ], vce (cluster id)
	outreg2 using "$path\Results\Table 5.xls", replace label  tex(frag) stats(coef se )   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	 
	 * Model 1. Including also region
	 
	 regress pcc pcc_SE ib3.outcome ib6.treatment ib2.region [aw =weight ], vce (cluster id)
	outreg2 using "$path\Results\Table 5.xls", append label  tex(frag) stats(coef se )   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	 

	 * Model 1. Including also methodology and discipline
	 
	regress pcc pcc_SE ib3.outcome ib6.treatment ib2.region i.discipline_g ib2.methodology [aw =weight ], vce (cluster id)
	outreg2 using "$path\Results\Table 5.xls", append label  tex(frag) stats(coef se )   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)


	*  Model 3. Including economic & political variables
	 regress pcc pcc_SE_sq ib3.outcome ib6.treatment [aw =weight ], vce (cluster id)
	outreg2 using "$path\Results\Table 5.xls", append label  tex(frag) stats(coef se )   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	 
	* Model 3. Including also region
	 
	 regress pcc pcc_SE_sq ib3.outcome ib6.treatment ib2.region [aw =weight ], vce (cluster id)
	outreg2 using "$path\Results\Table 5.xls", append label  tex(frag) stats(coef se )   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)
	 
	* Model 3. Including also methodology and discipline
	 
	regress pcc pcc_SE_sq ib3.outcome ib6.treatment ib2.region i.discipline_g ib2.methodology [aw =weight ], vce (cluster id)
	outreg2 using "$path\Results\Table 5.xls", append label  tex(frag) stats(coef se )   dec(4) alpha(0.01, 0.05,0.1) symbol(***, **, *)









